package com.lagou.test;

import com.lagou.dao.IUserDao;
import com.lagou.io.Resources;
import com.lagou.pojo.User;
import com.lagou.sqlSession.SqlSession;
import com.lagou.sqlSession.SqlSessionFactory;
import com.lagou.sqlSession.SqlSessionFactoryBuilder;
import org.dom4j.DocumentException;
import org.junit.Test;

import java.beans.PropertyVetoException;
import java.io.InputStream;
import java.util.List;

public class IPersistence {

    @Test
    public void test() throws Exception {
        //使用自己写的流读取类进行xml读取
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");

        //根据读取的xml信息创建sqlSession
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

//        //1. 不使用代理接口调用
//        User user = new User();
//        user.setId(1);
//        user.setUsername("lucy");
//        //这里的statementid是  UserMapper.xml中的  <namespace>.<id>
//        User oneUser = sqlSession.selectOne("user.selectOne", user);
//        System.out.println(oneUser.toString());
//
//        User user1 = new User();
//        List<User> users = sqlSession.selectList("user.selectAll", user1);
//        System.out.println(users);


        //2. 使用代理接口调用
        //代理对象调用接口中任意方法，都是执行invoke方法
        IUserDao userDao = sqlSession.getMapper(IUserDao.class);

        List<User> all = userDao.findAll();
        for (User item : all) {
            System.out.println(item);
        }

        System.out.println("单独调用测试");
        User daoUser = new User();
        daoUser.setId(1);
        daoUser.setUsername("lucy");
        System.out.println(userDao.findByCondition(daoUser));
    }
}
